﻿using CPPEI.Coolzon.Designer.Event.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CPPEI.Coolzon.UI;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// Interaction logic for StatusView.xaml
    /// </summary>
    public partial class StatusView : UserControl
    {
        private DesignView _designView;
        public StatusView()
        {
            InitializeComponent();
        }

        private double Floor(double? value)
        {
            return Math.Floor(value.HasValue ? value.Value : 0);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            userName.Content = UIContext.Current.UserName;
            workSpace.Content = UIContext.Current.LocalWorkSpace.LocalPath;
        }
        public void SelectPartChanged(Coolzon.UI.Part partInstance)
        {
            StackPanel sp = new StackPanel();
            sp.Orientation = Orientation.Horizontal;
            List<UIElement> hlList = new List<UIElement>();
            hlList.Add(new TextBlock(new Run(partInstance.XFullName)));
            Coolzon.UI.Part parent = partInstance.ParentPart;
            while (parent != null)
            {
                Hyperlink hl = new Hyperlink(new Run(parent.XFullName));
                hl.Click += Hl_Click;
                hlList.Add(new TextBlock(hl));
                parent = parent.ParentPart;
            }
            hlList.Reverse();
            int i = 0;
            foreach (var item in hlList)
            {
                sp.Children.Add(item);
                if (++i < hlList.Count)
                {
                    sp.Children.Add(new TextBlock(new Run(">")));
                }
            }
            partInfo.Content = sp;
        }

        private void Hl_Click(object sender, RoutedEventArgs e)
        {
            Hyperlink hl = sender as Hyperlink;
            string partName = ((System.Windows.Documents.Run)(hl.Inlines).FirstInline).Text;

            UIContext.Current.CurrentDesignView.API_Select(partName);
        }
        public void AttachDesignView(DesignView designView)
        {
            this._designView = designView;
            this._designView.Closed += _designView_Closed;
        }

        private void _designView_Closed(ScenePart scene)
        {
            partInfo.Content = null;
        }
    }
}
